/*
 * Copyright © 2019-2020  Whale Cloud, Inc. All Rights Reserved.
 *
 * Notice: Whale Cloud Inc copyrights this specification.
 * No part of this specification may be reproduced in any form or means,
 *  without the prior written consent of Whale Cloud Inc.
 *
 */
package com.shudai.largescreen.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WEB配置信息
 * @author mission
 * @since 2019/07/07 11:51
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
//    registry.addResourceHandler(PathUtil.fileUrlPrefix()+"**").addResourceLocations("file:"+ PathUtil.basePath()).setCachePeriod(0);
    registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

  }

//  @Bean
//  public CorsFilter corsFilter() {
//    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//    CorsConfiguration config = new CorsConfiguration();
//    config.setAllowCredentials(true);
//    config.addAllowedOrigin("*");//设置允许跨域请求的域名
//    config.addAllowedHeader("*"); //是否允许证书 不再默认开启
//    config.addAllowedMethod("*");//设置允许的方法
//    config.setMaxAge(3600L);//跨域允许时间
//
//
//    source.registerCorsConfiguration("/**", config);
//    return new CorsFilter(source);
//  }

  /**
   * 跨域配置
   */
  @Override
  public void addCorsMappings(CorsRegistry registry) {
    //对那些请求路径进行跨域处理
    registry.addMapping("/**")
            // 允许的请求头，默认允许所有的请求头
            .allowedHeaders("*")
            // 允许的方法，默认允许GET、POST、HEAD
            .allowedMethods("*")
            // 探测请求有效时间，单位秒
            .maxAge(1800)
            // 支持的域
            .allowedOrigins("*");
//            .allowedOriginPatterns("*");
  }
}